<template>
    <!-- 使用 Grid 网格布局，指定列数为2，高度占满全屏-->
    <div class="grid grid-cols-2 h-screen">
        <!--    适配移动端，占两列，order用于指定排列顺序，md用于适配非移动端-->
        <div
            class="col-span-2 order-2 p-10 md:col-span-1 md:order-1 bg-blue-500 animate__animated animate__bounceInLeft animate__fast">
            <!-- 指定为 flex 布局，并设置为屏幕垂直水平居中，高度为 100% -->
            <div class="flex justify-center items-center h-full flex-col">
                <h2 class="font-bold text-4xl mb-7 text-white">Weblog 博客登录</h2>
                <p class="text-white">一款由 Spring Boot + Mybaits Plus + Vue 3.2 + Vite 4 开发的前后端分离博客。</p>
                <!-- 指定图片宽度为父级元素的 1/2 -->
                <img src="@/assets/developer.png" class="w-1/2">
            </div>
        </div>
        <div
            class="col-span-2 order-1 md:col-span-1 md:order-2 bg-white animate__animated animate__bounceInRight animate__fast">
            <!--垂直排列-->
            <div class="flex justify-center items-center h-full flex-col">
                <!-- 大标题，设置字体粗细、大小、下边距 -->
                <h1 class="font-bold text-4xl mb-5">欢迎回来</h1>
                <!-- 设置 flex 布局，内容垂直水平居中，文字颜色，以及子内容水平方向 x 轴间距 -->
                <div class="flex items-center justify-center mb-7 text-gray-400 space-x-2">
                    <!-- 左边横线，高度为 1px, 宽度为 16，背景色设置 -->
                    <span class="h-[1px] w-16 bg-gray-200"></span>
                    <span>账号密码登录</span>
                    <!-- 右边横线 -->
                    <span class="h-[1px] w-16 bg-gray-200"></span>
                </div>
                <!-- 引入 Element Plus 表单组件，移动端设置宽度为 5/6，PC 端设置为 2/5 -->
                <el-form class="w-5/6 md:w-2/5" ref="formRef" :rules="rules" :model="form">
                    <el-form-item prop="username">
                        <el-input size="large" v-model="form.username" placeholder="请输入账号" :prefix-icon="User"
                            clearable />
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input size="large" v-model="form.password" type="password" placeholder="请输入密码"
                            :prefix-icon="Lock" clearable show-password />
                    </el-form-item>
                    <el-form-item>
                        <el-button :loading="loading" type="primary" size="large" class="w-full"
                            @Click="onSubmit">登录</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>

    </div>
</template>
<script setup>
// 引入 Element Plus 中的用户、锁图标
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/admin/user';
import { ref, reactive, onMounted, onBeforeUnmount } from "vue";
import { useRouter } from 'vue-router';
import { showMessage } from '@/composables/util';
import { setToken } from '@/composables/cookie';
import { useUserStore } from '@/stores/user'
const userStore = useUserStore()
/**
 * 登录按钮加载loading
 */
const loading = ref(false)
const router = useRouter();
const formRef = ref(null)
const form = reactive({
    username: '',
    password: ''
})
//表单校验
const rules = {
    username: [
        { required: true, message: '请输入账号', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
    ]

}
/**
 * 回车按键，执行登录
 */
function onKeyUp(e) {
    if (e.key === 'Enter') {
        onSubmit()
    }
}
/**
 * 添加键盘监听
 */
onMounted(() => {
    document.addEventListener('keyup', onKeyUp)
})
/**
 * 移除键盘监听
 */
onBeforeUnmount(() => {
    document.removeEventListener('keyup', onKeyUp)
})
/**
 * 表单提交
 */
const onSubmit = () => {
    /**
 * 验证表单
 * 
 */
    formRef.value.validate((valid) => {
        if (!valid) {
            console.log("表单验证不通过")
            return false
        }
        loading.value = true
        console.log("触发登录")
        /**
         * 请求接口
         */
        login(form).then(res => {
            console.log(res)
            //如果登陆成功，跳转到首页
            if (res.success) {
                let token = res.data.token
                setToken(token)
                // 获取用户信息，并存储到全局状态中
                userStore.setUserInfo()
                showMessage("登录成功")
                router.push('/admin/index')
            } else {
                showMessage(res.message, 'error')
            }
        }).finally(() => {
            loading.value = false
        })
    })

}

</script>